<?php

namespace App\Http\Controllers\Api;

use App\Model\AdminLog;
use App\Model\DiamondRecord;
use App\Model\Number;
use App\Model\OperationLogs;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use phpDocumentor\Reflection\DocBlock\Tags\Example;

class DiamondRecordController extends BaseController
{
    public function __construct(Request $request, DiamondRecord $diamondRecord)
    {
        parent::__construct($request);
        $this->model = $diamondRecord;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        return $this->toJson(self::SUCCESS, $this->model->getList($request));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Number $number)
    {
        $userIdArr = explode(',', $request->all()['user_id']);
        $uuidArr = explode(',', $request->all()['uuid']);
        $validator = Validator::make($request->all(),[
            'user_id' => [
                'bail', 'required', 'string'
            ],
            'uuid' => [
                'bail', 'required', 'string'
            ],
            'type' => 'bail|required|integer',Rule::in(1,2),
            'change_diamond' => 'bail|required|integer|min:1|max:99999999',
            'sys_notice' => 'required_if:type,2|string'
        ],[
            'user_id.required' => '用户不可为空',
//            'user_id.exists' => '用户不存在',
//            'uuid.exists' => '用户不存在',
            'type.required' => '商品类型不可为空',
            'change_diamond.required' => '金额不可为空',
            'change_diamond.integer' => '金额必须为整数',
            'change_diamond.min' => '金额最小值为1',
            'change_diamond.max' => '金额最大值为99999999',
            'sys_notice.required_if' => '系统通知不可为空'
        ]);

        if($validator->fails()){
            return $this->toJson(self::PARAM_ERROR, [],$validator->errors()->all()[0]);
        }

        $changeDiamond = 0;
        $desc = '';
        if($request->input('type') === 1){
            $changeDiamond = '-'. $request->input('change_diamond');
            $desc = '扣款';
        } elseif ($request->input('type') === 2) {
            $changeDiamond = $request->input('change_diamond');
            $desc = '赠送';
        }

        //起始值
        $start = 0;
        foreach ($userIdArr as $key => $value) {
            $res = $this->remoteSetDiamond($changeDiamond, $value);
            if($res['code'] === 0) {
                $number = $number->getNicknameByUserId($value);

                $arr = $request->all();
                $arr['user_id'] = $value;
                $arr['uuid'] = $uuidArr[$key];
                $diamondRecord = $this->model->createDataAll($arr, $changeDiamond,$number['nickname']);
                if($request->input('type') == 2) Artisan::call('send:custom:msg',[
                    'type' => 0,
                    'all' => 0,
                    'msg' => $request->input('sys_notice'),
                    'user' => $uuidArr[$key]
                ]);
                OperationLogs::createData($request, OperationLogs::DIAMOND_RECORD, $desc, $uuidArr[$key]);
            }else {
                $start++;
                //todo write to log
                write_log('diamondChange', 'error:', "【uuid:{$uuidArr[$key]}】");
            }
        }

        if($start == 0) {
            return $this->toJson(self::SUCCESS, []);
        }else {
            return $this->toJson(self::ERROR, [], '操作有误,请查看日志(diamondChange)');
        }
    }

    private function remoteSetDiamond(int $value, int $user_id){
        return curl_request(getKkaudioApiUrl().'/inner/finance/back/pay',[ 'user_id' => $user_id,'diamond_balance' => $value ],'json','POST');
    }
}
